package model

import (
	"gorm.io/gorm"
	__ "user-srv/basic/proto"
)

type Video struct {
	gorm.Model
	Title  string `gorm:"type:varchar(20);not null"`
	Url    string `gorm:"type:varchar(255);not null"`
	Num    int    `gorm:"type:int;not null"`
	Info   string `gorm:"type:varchar(20);not null"`
	Status int    `gorm:"type:int(11);not null"`
	Types  int    `gorm:"type:int(11);not null"`
	Image  string `gorm:"type:varchar(255);not null"`
}

// 获取信息
func (v *Video) GetOnfile(db *gorm.DB) error {
	return db.Model(&v).Where("title = ?", v.Title).Limit(1).Find(&v).Error
}

// 添加的方法
func (v *Video) Create(db *gorm.DB) error {
	return db.Model(&v).Create(&v).Error
}

// 详情查看
func (v *Video) GetShow(db *gorm.DB, id int) error {
	return db.Model(&v).Where("id = ?", id).Limit(1).Find(&v).Error
}

// 模糊搜索
func (v *Video) GetLike(db *gorm.DB, title string) error {
	return db.Model(&v).Where("title like ?", "%"+title+"%").Find(&v).Error
}

// 分页查询
func (v *Video) GetCover(db *gorm.DB, page, size int) (videolist []*__.Videolist, err error) {
	Ppage := page
	if page <= 0 {
		page = 1
	}

	pageSize := size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (Ppage - 1) * pageSize
	err = db.Model(&v).Offset(offset).Limit(pageSize).Find(&videolist).Error
	return videolist, err
}
